package com.bus.admin.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bus.admin.bean.PaiChe;
import com.bus.admin.service.PaiCheService;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bus.admin.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bus.admin.bean.Orderbus;
import com.bus.admin.bean.result.ResultPage;
import com.bus.admin.controller.model.ApiResultModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2020-04-01
 */
@Controller
@RequestMapping("/bus/orderbus")
public class OrderbusController extends BaseController{
     /** 主页面 */
    @RequestMapping("main.html")
    public String main(){
       return  "orderbus/OrderbusMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "/addOrderbusHtml")
    public String addOrderbusHtml( ){
             return "orderbus/OrderbusAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/editOrderbusHtml")
    public String editOrderbusHtml(Integer id ,Model model ){
           model.addAttribute("orderbus",orderbusService.selectOrderbusByIdOrderbus(id));
             return "orderbus/OrderbusEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/showOrderbusHtml")
    public String showOrderbusHtml(Integer id ,Model model ){
           model.addAttribute("orderbus",orderbusService.selectOrderbusByIdOrderbus(id));
             return "orderbus/OrderbusShow";
    }
    /** 审核页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/examineHtml")
    public String examineHtml(Integer id ,Model model ){
        model.addAttribute("orderbus",orderbusService.selectOrderbusByIdOrderbus(id));
             return "orderbus/OrderbusExamine";
    }


    /** 分页查询Orderbus
     * @param page  页码
     * @param limit  查几条
     * @param orderbus  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/findOrderbusByPage")
    @ResponseBody
    public ResultPage findOrderbusByPage(Integer page, Integer limit,Orderbus orderbus){
             return orderbusService.findOrderbusByPage(page,limit,orderbus);
    }

    /** 添加Orderbus
     * @param orderbus  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/addOrderbus")
    @ResponseBody
    public ApiResultModel saveOrderbus(Orderbus orderbus  ){
             return orderbusService.addOrderbus(orderbus);
    }

    /** 编辑Orderbus
     * @param orderbus  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editOrderbus")
    @ResponseBody
    public ApiResultModel editOrderbus(Orderbus orderbus  ){
        if(!StrUtil.isBlank(orderbus.getPaytime1())){
            orderbus.setPaytime1(DateUtil.parseDateTime(orderbus.getPaytime1()).getTime()+"");
        }
        if(!StrUtil.isBlank(orderbus.getPaytime2())){
            orderbus.setPaytime2(DateUtil.parseDateTime(orderbus.getPaytime2()).getTime()+"");
        }
        if(!StrUtil.isBlank(orderbus.getPaytime3())){
            orderbus.setPaytime3(DateUtil.parseDateTime(orderbus.getPaytime3()).getTime()+"");
        }
             return orderbusService.editOrderbus(orderbus);
    }



    /** 删除一条Orderbus
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/delOrderbus")
    @ResponseBody
    public ApiResultModel delOrderbus( Integer id  ){
        Orderbus orderbus= orderbusService.getById(id);
        switch (orderbus.getPayType()) {
            case 0: {  //全款 派车单.
               String orderNum= orderbus.getOrdernum();
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("orderNum",orderNum);
                paiCheService.remove(queryWrapper);
                break;
            }
            case 1: {  //定金派车单
                String dingjinNum= orderbus.getDingjinNum();
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("orderNum",dingjinNum);
                paiCheService.remove(queryWrapper);
                break;
            }

        }
        return orderbusService.delOrderbus(id);
    }


}
